$(function () {
  // ****************************************编辑用户


  let layer = layui.layer;
  let form = layui.form;

  let userInfo;
  // 数据回填=======================================================
  function write() {
    form.val("editForm", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
      "username": userInfo.username, // "name": "value"
      "nickname": userInfo.nickname,
      "email": userInfo.email
    });
  }
  // 获取用户详情
  function getUserDate() {
    let params = location.search.split('?')[1]
    console.log(params);
    $.ajax({
      type: 'get',
      url: `/admin/users/${params}`,
      success: function (res) {
        console.log(res);
        let { data } = res;
        userInfo = data;
        console.log(userInfo);
        write()
      }
    })
  }
  getUserDate()
// 表单提交事件===============================================

  form.on('submit(*)', function (data) {
    let queryDate = data.field;
    queryDate.id = userInfo.id;
    console.log(queryDate) //当前容器的全部表单字段，名值对形式：{name: value}
    $.ajax({
      url: '/admin/users',
      type: 'put',
      data: queryDate,
      success: function (res) {
        if (res.status == 0) {
          layer.msg(res.message)
          // window.location='http://127.0.0.1:5500/index.html'
          location.href = '../../../user/user.html'
        }
       
      }
    })
    return false;
  });








})